package com.viaas.certification.image.action;

import com.comphenix.protocol.*;
import com.comphenix.protocol.events.*;
import com.comphenix.protocol.wrappers.BlockPosition;
import com.comphenix.protocol.wrappers.EnumWrappers;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.Random;
import java.util.Set;
import java.util.HashSet;

public class AIPlayerMiner {
    private final Player player;
    private final Plugin plugin;
    private boolean isMining = false;
    private final Random random = new Random();

    private static final Set<Material> MINERALS = new HashSet<>();

    static {
        MINERALS.add(Material.DIAMOND_ORE);
        MINERALS.add(Material.GOLD_ORE);
        MINERALS.add(Material.IRON_ORE);
        MINERALS.add(Material.REDSTONE_ORE);
        MINERALS.add(Material.COAL_ORE);
        MINERALS.add(Material.LAPIS_ORE);
    }

    public AIPlayerMiner(Player player, Plugin plugin) {
        this.player = player;
        this.plugin = plugin;
    }

    // 开启 AI 自动挖矿
    public void startMining() {
        if (isMining) return;
        isMining = true;

        player.sendMessage(ChatColor.GREEN + "AI 开始挖矿！");

        new BukkitRunnable() {
            @Override
            public void run() {
                if (!isMining) {
                    cancel();
                    return;
                }

                Block nearestOre = findNearestOre(player.getLocation());
                if (nearestOre != null) {
                    moveToBlock(nearestOre.getLocation());
                } else {
                    player.sendMessage(ChatColor.GRAY + "没找到矿石，换个地方...");
                }
            }
        }.runTaskTimer(plugin, 0, 100);
    }

    // 停止 AI 挖矿
    public void stopMining() {
        isMining = false;
        player.sendMessage(ChatColor.RED + "AI 停止挖矿！");
    }

    // 寻找最近的矿石
    private Block findNearestOre(Location loc) {
        World world = loc.getWorld();
        Block nearestOre = null;
        double minDistance = Double.MAX_VALUE;

        int radius = 10;
        for (int x = -radius; x <= radius; x++) {
            for (int y = -radius; y <= radius; y++) {
                for (int z = -radius; z <= radius; z++) {
                    Block block = world.getBlockAt(loc.getBlockX() + x, loc.getBlockY() + y, loc.getBlockZ() + z);
                    if (MINERALS.contains(block.getType())) {
                        double distance = loc.distance(block.getLocation());
                        if (distance < minDistance) {
                            minDistance = distance;
                            nearestOre = block;
                        }
                    }
                }
            }
        }
        return nearestOre;
    }

    // 让 AI 走向矿石
    private void moveToBlock(Location blockLocation) {
        player.teleport(blockLocation.add(0, 1, 0));
        new BukkitRunnable() {
            @Override
            public void run() {
                if (player.getLocation().distance(blockLocation) < 1.5) {
                    breakBlock(player, blockLocation.getBlock());
                }
            }
        }.runTaskLater(plugin, 20);
    }

    // 让 AI 真实破坏方块
    private void breakBlock(Player player, Block block) {
        if (MINERALS.contains(block.getType())) {
            player.sendMessage(ChatColor.AQUA + "AI 正在挖掘: " + block.getType());

            // 发送模拟左键点击方块的网络包
            ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
            PacketContainer blockDigPacket = protocolManager.createPacket(PacketType.Play.Client.BLOCK_DIG);
            blockDigPacket.getBlockPositionModifier().write(0, new BlockPosition(block.getX(), block.getY(), block.getZ()));
            blockDigPacket.getEnumModifier(EnumWrappers.PlayerDigType.class, 1)
                    .write(0, EnumWrappers.PlayerDigType.START_DESTROY_BLOCK);

            try {
                protocolManager.sendServerPacket(player, blockDigPacket);
            } catch (Exception e) {
                e.printStackTrace();
            }

            new BukkitRunnable() {
                @Override
                public void run() {
                    block.breakNaturally(player.getInventory().getItemInMainHand());
                }
            }.runTaskLater(plugin, 40);
        }
    }
}
